package net.neptech.node;

import com.alibaba.cloud.ai.graph.OverAllState;
import com.alibaba.cloud.ai.graph.action.NodeAction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
/**
 * @Author HuangZhengmin
 * @desc  紧急分诊节点 - 处理危急和严重症状
 * @Date 2025/8/19 17:41
 * @Version 1.0
 */
@Component
public class EmergencyTriageNode implements NodeAction {
  private static final Logger logger = LoggerFactory.getLogger(EmergencyTriageNode.class);

  @Autowired
  private ChatClient chatClient;

  @Override
  public Map<String, Object> apply(OverAllState state) {
    logger.info("进入紧急分诊节点");
    Map<String, Object> result = new HashMap<>();

    // 获取症状和严重程度
    String symptoms = (String) state.value("standardized_symptoms").orElse("");
    String severity = (String) state.value("severity").orElse("critical");

    // 生成紧急处理建议
    String prompt = String.format(
            "患者症状: %s\n严重程度: %s\n" +
                    "请提供紧急处理建议，包括:\n" +
                    "1. 立即需要采取的措施\n" +
                    "2. 推荐就诊的急诊科室\n" +
                    "3. 转运注意事项\n" +
                    "建议需简洁明了，突出紧急性。", symptoms, severity);

    String triageResult = chatClient.prompt()
            .user(prompt)
            .call()
            .content();

    // 更新状态
    result.put("emergency_triage_result", triageResult);
    result.put("risk_level", "high_risk");
    result.put("final_recommendation", "【紧急提示】" + triageResult);

    logger.warn("紧急情况已处理: {}", severity);
    return result;
  }
}
